FROM ubuntu:20.04   as   build

ARG gitUrl="https://git.linker.cc/omvision/backend/back_visionos/wvp.git"
ARG zlmGitUrl="https://git.linker.cc/omvision/backend/back_visionos/zlmediakit.git"

RUN export DEBIAN_FRONTEND=noninteractive &&\
        apt-get update && \
        apt-get install -y --no-install-recommends openjdk-11-jre git maven nodejs npm build-essential \
        cmake ca-certificates openssl ffmpeg &&\
        mkdir -p /opt/wvp/config /opt/wvp/heapdump /opt/wvp/config /opt/assist/config /opt/assist/heapdump /opt/media/www/record


RUN cd /home && \
        git clone "${gitUrl}"
RUN cd /home/wvp/web_src && \
        npm install && \
        npm run build
RUN cd /home/wvp && \
        mvn clean package -Dmaven.test.skip=true && \
        cp /home/wvp/target/*.jar /opt/wvp/ && \
        cp /home/wvp/src/main/resources/application.yml /opt/wvp/config/application.yml

RUN cd /home && \
        git clone "${zlmGitUrl}"
RUN cd /home/ZLMediaKit && \
        git submodule update --init --recursive && \
        mkdir -p build release/linux/Release/ &&\
        cd build && \
        cmake -DCMAKE_BUILD_TYPE=Release .. && \
        make -j4 && \
        rm -rf ../release/linux/Release/config.ini && \
        cp -r ../release/linux/Release/* /opt/media

RUN cd /opt/wvp && \
        echo '#!/bin/bash' > run.sh && \
        echo 'cd /opt/assist' >> run.sh && \
       # echo 'nohup java ${ASSIST_JVM_CONFIG} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/assist/heapdump/ -jar *.jar --spring.config.location=/opt/assist/config/application.yml --userSettings.record=/opt/media/www/record/  --media.record-assist-port=18081 ${ASSIST_CONFIG} &' >> run.sh && \
        echo 'nohup /opt/media/MediaServer -d -m 3 &' >> run.sh && \
        echo 'cd /opt/wvp' >> run.sh && \
        echo 'java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/wvp/heapdump/ -jar *.jar --spring.config.location=/opt/wvp/config/application.yml' >> run.sh && \
        chmod +x run.sh

FROM ubuntu:20.04

EXPOSE 32000-32200/tcp
EXPOSE 32000-32200/udp

ENV LC_ALL zh_CN.UTF-8

RUN export DEBIAN_FRONTEND=noninteractive &&\
        apt-get update && \
        apt-get install -y --no-install-recommends openjdk-11-jre ca-certificates ffmpeg language-pack-zh-hans && \
        apt-get autoremove -y && \
        apt-get clean -y && \
        rm -rf /var/lib/apt/lists/*dic

COPY --from=build /opt /opt
WORKDIR /opt/wvp
CMD ["sh", "run.sh"]
